﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Ny.CenterServers.Test;
using Ny.CenterServers.Tests.Dto;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Abp.Runtime.Session;
using Microsoft.AspNetCore.Http;
using Ny.CenterServers.Permissions;

namespace Ny.CenterServers.Tests
{
    [Authorize]
    public class TestAppService : ITestAppService
    {
        private readonly IRepository<TestModel,Guid> _testRepository;
        IAbpSession _session;
        public TestAppService(IRepository<TestModel,Guid> testRepository, IAbpSession abpSession)
        {
            _testRepository = testRepository;
            _session = abpSession;
        }

        /// <summary>
        /// 获取str
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetString()
        {
            var id = _session.UserId;
            //var t = new JwtSecurityTokenHandler().ReadJwtToken().Claims;
            return "hello,world!";
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <returns></returns>
        [AbpAuthorize("Action.Test.Create")]
        public TestModel Create()
        {
            
            return new TestModel { Name = "test", Id = Guid.NewGuid(), CreationTime = DateTime.Now, CreationName = "test" };
        }

        public List<TestModel> GetTestModel()
        {
            var s=_testRepository.GetAll().ToList();
            return s;
        }

        public async Task<ListResultDto<TestModelDto>> GetTestModelEx()
        {
            var s = await _testRepository.GetAllListAsync();
            return new ListResultDto<TestModelDto>(
                AutoMapper.Mapper.Map<List<TestModelDto>>(s));
        }
    }
}
